<?php
if (!defined('APPPATH')) exit('No direct script access allowed');

include_once(APPPATH.'libraries/api/core/MySQL.php');
include_once(APPPATH.'libraries/api/core/Services.php');

class Menu extends Services {
    
	public static function menuRecordUpdate($menu_id, $column, $column_type, $value) {
		if(parent::checkArguments(array($menu_id, $column, $column_type, $value))) {
			$query = "UPDATE `menu` SET `$column` = " . MySQL::getColumnValueNotation($column_type, $value) . " WHERE `menu_id` = $menu_id; ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}    

	public static function menuGet($menu_id) {
		if(parent::checkArguments($menu_id)) {
			$query = "SELECT `menu_id`, `parent_menu_id`, `language_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
					  WHERE `menu_id` = $menu_id AND `published` = 1 AND `deleted` != 1
					  ORDER BY `ordering`; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}

	public static function menuSet($parent_menu_id, $language_id, $name, $title) {
		if(parent::checkArguments(array($parent_menu_id, $language_id, $name, $title))) {
                        $query = "SET @lastOrder = (SELECT `ordering` FROM `menu` WHERE `parent_menu_id` = $parent_menu_id ORDER BY `ordering` DESC LIMIT 0,1);
                                  INSERT INTO `menu` (`parent_menu_id`, `language_id`, `name`, `title`, `ordering`, `published`, `deleted`) 
                                  VALUES ($parent_menu_id, $language_id, '$name', '$title', (SELECT COALESCE(@lastOrder,0)) + 1, 1, 0); ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
    
    // sets menu which does not have parent on last ordering position and returns newly set record's ID
	public static function menuRootSet($language_id, $name, $title) {
		if(parent::checkArguments(array($language_id, $name, $title))) {
                        $query = "SET @lastOrder = (SELECT `ordering` FROM `menu` WHERE `parent_menu_id` IS NULL ORDER BY `ordering` DESC LIMIT 0,1);
                                  INSERT INTO `menu` (`parent_menu_id`, `language_id`, `name`, `title`, `ordering`, `published`, `deleted`) 
                                  VALUES (NULL, $language_id, '$name', '$title', (SELECT COALESCE(@lastOrder,0)) + 1, 1, 0); ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return $result['last_insert_id'][1];
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static function menuUpdate($menu_id, $parent_menu_id, $language_id, $name, $title) {
		if(parent::checkArguments(array($menu_id, $parent_menu_id, $language_id, $name, $title))) {
			$query = "UPDATE `menu` SET `parent_menu_id` = $parent_menu_id,
                                                    `name` = '$name', 
                                                    `title` = '$title'
                                  WHERE `menu_id` = $menu_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
        
	public static function menuRootUpdate($menu_id, $language_id, $name, $title) {
		if(parent::checkArguments(array($menu_id, $language_id, $name, $title))) {
			$query = "UPDATE `menu` SET `language_id` = $language_id,
                                                    `name` = '$name', 
                                                    `title` = '$title'
                                  WHERE `menu_id` = $menu_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}        

	public static function menuList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
				  WHERE `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function menuLanguageList($language_id) {
		if(parent::checkArguments($language_id)) {
                        $query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
                                          WHERE `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
			$result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}        
        
	public static function menuParentList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
				  WHERE `parent_menu_id` IS NULL AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function menuChildrenList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
				  WHERE parent_menu_id IS NOT NULL AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function menuLanguageChildrenList($language_id) {
		if(parent::checkArguments($language_id)) {
                        $query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
                                          WHERE parent_menu_id IS NOT NULL AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
			$result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
    
    // restaurant customization
	public static function menuFirstBornChildList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `language_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
				  WHERE parent_menu_id IN (SELECT `menu_id` FROM `menu` WHERE `parent_menu_id` IS NULL); ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
    
    // restaurant customization
	public static function menuSecondBornChildList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `language_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
                  WHERE parent_menu_id IN (SELECT `menu_id` FROM `menu` 
                                           WHERE parent_menu_id IN (SELECT `menu_id` FROM `menu` 
                                                                    WHERE `parent_menu_id` IS NULL)); ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
	
	public static function menuRootList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `language_id`, `name`, `title`, `ordering`, `published`, `flag` & 1 AS `union` FROM `menu` 
				  WHERE parent_menu_id IS NULL AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function menuLanguageRootList($language_id) {
		if(parent::checkArguments($language_id)) {
                        $query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published`, `flag` & 1 AS `union` FROM `menu` 
                                          WHERE parent_menu_id IS NULL AND `language_id` = $language_id AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
			$result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}     
        
	public static function menuLeafList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
				  WHERE `menu_id` NOT IN (SELECT `parent_menu_id` FROM `menu` WHERE `parent_menu_id` IS NOT NULL) AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function menuLanguageLeafList($language_id) {
                if(parent::checkArguments($language_id)) {
                    $query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
                                      WHERE `menu_id` NOT IN (SELECT `parent_menu_id` FROM `menu` WHERE `parent_menu_id` IS NOT NULL) AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
			$result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}        
        
	public static function menuStumpList() {
		$query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
				  WHERE `menu_id` NOT IN (SELECT `parent_menu_id` FROM `menu` WHERE `parent_menu_id` IS NOT NULL) 
                                  AND `parent_menu_id` IS NOT NULL AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}        
	
	public static function subMenuList($parent_menu_id) {
		if(parent::checkArguments($parent_menu_id)) {
			$query = "SELECT `menu_id`, `parent_menu_id`, `name`, `title`, `ordering`, `published` FROM `menu` 
					  WHERE `parent_menu_id` = $parent_menu_id AND `published` = 1 AND `deleted` != 1 ORDER BY `ordering`; ";
            $result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
        
	public static function menuDelete($menu_id) {
		if(parent::checkArguments($menu_id)) {
			$query = "DELETE FROM `multimedia` WHERE `menu_id` = $menu_id;
                      DELETE FROM `multimedia` WHERE `menu_id` IN (SELECT `menu_id` FROM `menu` WHERE `parent_menu_id` = $menu_id);
                      DELETE FROM `content` WHERE `menu_id` = $menu_id;
                      DELETE FROM `content` WHERE `menu_id` IN (SELECT `menu_id` FROM `menu` WHERE `parent_menu_id` = $menu_id);
                      DELETE FROM `menu` WHERE `parent_menu_id` = $menu_id;
                      DELETE FROM `menu` WHERE `menu_id` = $menu_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
        
	public static function menuOrder($menu_id, $direction, $opposite) {
		if(parent::checkArguments(array($menu_id, $direction, $opposite))) {
            $query = "SET @parentId = (SELECT `parent_menu_id` FROM `menu` WHERE `menu_id` = $menu_id);
                      SET @currentOrder = (SELECT `ordering` FROM `menu` WHERE `menu_id` = $menu_id); 
                      SET @minOrder = (SELECT MIN(`ordering`) FROM `menu` WHERE `parent_menu_id` = @parentId); 
                      SET @maxOrder = (SELECT MAX(`ordering`) FROM `menu` WHERE `parent_menu_id` = @parentId);
                      SET @languageId = (SELECT `language_id` FROM `menu` WHERE `menu_id` = $menu_id);
                      UPDATE `menu` SET `ordering` = (SELECT IF ('$direction' = '+', 
                                                        (SELECT IF (@currentOrder = @minOrder OR @currentOrder - 1 = @minOrder, @minOrder, @currentOrder - 1)), 
                                                        (SELECT IF (@currentOrder = @maxOrder OR @currentOrder + 1 = @maxOrder, @maxOrder, @currentOrder + 1)))) 
                      WHERE `menu_id` = $menu_id;
                      UPDATE `menu` SET `ordering` = @currentOrder 
                      WHERE `parent_menu_id` = @parentId AND `ordering` = (SELECT IF ('$direction' = '+', @currentOrder - 1, @currentOrder + 1))
                      AND `menu_id` != $menu_id AND `language_id` = @languageId; ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
        
	public static function menuRootOrder($menu_id, $direction, $opposite) {
		if(parent::checkArguments(array($menu_id, $direction, $opposite))) {
			$query = "SET @currentOrder = (SELECT `ordering` FROM `menu` WHERE `menu_id` = $menu_id); 
                      SET @minOrder = (SELECT MIN(`ordering`) FROM `menu` WHERE `parent_menu_id` IS NULL); 
                      SET @maxOrder = (SELECT MAX(`ordering`) FROM `menu` WHERE `parent_menu_id` IS NULL);
                      SET @languageId = (SELECT `language_id` FROM `menu` WHERE `menu_id` = $menu_id);
                      UPDATE `menu` SET `ordering` = (SELECT IF ('$direction' = '+', 
                                                        (SELECT IF (@currentOrder = @minOrder OR @currentOrder - 1 = @minOrder, @minOrder, @currentOrder - 1)), 
                                                        (SELECT IF (@currentOrder = @maxOrder OR @currentOrder + 1 = @maxOrder, @maxOrder, @currentOrder + 1)))) 
                      WHERE `menu_id` = $menu_id;
                      UPDATE `menu` SET `ordering` = @currentOrder 
                      WHERE `parent_menu_id` IS NULL AND `ordering` = (SELECT IF ('$direction' = '+', @currentOrder - 1, @currentOrder + 1))
                      AND `menu_id` != $menu_id AND `language_id` = @languageId; ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}        

}

?>